﻿using Apple.BLL.Dto.DataDto;
using Apple.BLL.Manager;
using Apple.Helper;
using HZY.Framework.DependencyInjection;
using MiniExcelLibs;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apple.SprayingProcessSystem
{
    public partial class PageReportManage : UIPage, ISingletonSelfDependency
    {
        private readonly DataManager _dataManager;

        public PageReportManage(DataManager dataManager)
        {
            InitializeComponent();
            _dataManager = dataManager;

            this.dtp_Start.Value = DateTime.Now.AddDays(-1);
            this.dtp_End.Value = DateTime.Now;

            // 不自动生成列名
            this.dgv_Data.AutoGenerateColumns = false;
        }

        private BaseResult<QueryDataResultDto> res;
        private async void btn_QueryData_Click(object sender, EventArgs e)
        {
            DateTime t1 = this.dtp_Start.Value;
            DateTime t2 = this.dtp_End.Value;
            if (t1 >= t2)
            {
                UIMessageTip.ShowWarning("开始时间不可以大于结束时间");
                return;
            }

            QueryDataDto queryDataDto = new QueryDataDto() { StartTime = t1, EndTime = t2 };

            res = await _dataManager.GetDataListByTimeAsync(queryDataDto);

            if (res.Status == SystemEnums.Result.Success)
            {
                // 设置分页总数
                pgn_Data.TotalCount = res.Data.Count;
                pgn_Data.PageSize = 15;
                // 设置当前页
                pgn_Data.ActivePage = 1;
            }
        }

        /// <summary>
        /// 分页发生改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pagingSource"></param>
        /// <param name="pageIndex">当前页索引1开始</param>
        /// <param name="count">一页显示多少条</param>
        private void pgn_Data_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            var data = res.Data.Skip((pageIndex - 1) * count).Take(count).ToList();

            this.dgv_Data.DataSource = null;
            this.dgv_Data.DataSource = data;
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ExportData_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel 文件(*.xlsx)|*.xlsx";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var filePath = saveFileDialog.FileName;
                    var rows = this.dgv_Data.DataSource as List<QueryDataResultDto>;

                    MiniExcel.SaveAs(filePath, rows);
                }
                catch (Exception ex)
                {
                    UIMessageTip.ShowError("导出失败：" + ex.Message);
                    LogExtension.ShowMessage("导出失败：" + ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
                }


            }
        }

        #region 窗体切换卡顿处理
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams paras = base.CreateParams;
                paras.ExStyle |= 0x02000000;
                return paras;
            }
        }
        #endregion
    }
}
